package com.health.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.health.entity.UserModel;

import com.health.service.UserService;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import javax.annotation.Resource;

@RestController
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/api/saveUser")
    public R saveUser(UserModel user){
        UserModel userByUserName = userService.getOne(new LambdaQueryWrapper<UserModel>().eq(UserModel::getUserName, user.getUserName()));
        if (userByUserName != null){
            return R.failed("该用户已存在");
        }
        user.setRole(1);
        userService.save(user);
        return R.ok(null);
    }

    @DeleteMapping("/api/deleteUserByIds/{userIds}")
    public R deleteUserByIds(@PathVariable String userIds){
        String[] ids = userIds.split(",");
        userService.removeByIds(Arrays.asList(ids));
        return R.ok(null);
    }

    @PostMapping("/api/updateUser")
    public R updateUser(UserModel user){
        userService.updateById(user);
        return R.ok(null);
    }

    @GetMapping("/api/getUserList")
    public R getUserList(Page page){
        IPage<UserModel> resultPage = userService.getUserList(page);
        return R.ok(resultPage);
    }

    @GetMapping("/api/getUserById")
    public R getUserById(Integer id){
        return R.ok(userService.getById(id));
    }
}
